package com.atguigu.edu.pubshier.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.edu.pubshier.beans.DataInfo;
import com.atguigu.edu.pubshier.beans.ResponseVO;
import com.atguigu.edu.pubshier.beans.TradeProvinceOrderAmount;
import com.atguigu.edu.pubshier.service.TradeStatsService;
import com.atguigu.edu.pubshier.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author: techyang
 * @since: 2023-10-16 21:22
 * @description: 交易主题
 **/
@RestController
public class TradeStatsController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/subject")
    public String getSubjectAmount(@RequestParam(value = "date",defaultValue = "0") Integer date,@RequestParam(value = "type") String type){
        if(date == 0){
            date = DateFormatUtil.now();
        }
        List<Map<String, Object>> subjectAmountList = tradeStatsService.getSubject(date,type);
        // 学科集合
        List<String> subjectList = subjectAmountList.stream().map(mp -> (String)mp.get("subject_name")).collect(Collectors.toList());
        // 统计结果集合
        List<BigDecimal> dataList = subjectAmountList.stream().map(mp -> (BigDecimal)mp.get("value")).collect(Collectors.toList());

        ResponseVO<DataInfo> dataInfoResponseVO = new ResponseVO<>();
        dataInfoResponseVO.setStatus(0);
        DataInfo dataInfo = new DataInfo();
        dataInfo.setCategories(subjectList);
        List<DataInfo.Sery> seryList = new ArrayList<>();
        DataInfo.Sery sery = new DataInfo.Sery("学科",dataList);
        seryList.add(sery);
        dataInfo.setSeries(seryList);
        dataInfoResponseVO.setData(dataInfo);
        return JSON.toJSONString(dataInfoResponseVO);
    }

    @RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value =  "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceOrderAmountList = tradeStatsService.getProvinceAmount(date);
        StringBuilder jsonB = new StringBuilder();
        jsonB.append("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceOrderAmountList.size(); i++) {
            TradeProvinceOrderAmount provinceOrderAmount = provinceOrderAmountList.get(i);
            jsonB.append("{\"name\": \""+provinceOrderAmount.getProvinceName()+"\",\"value\": "+provinceOrderAmount.getOrderAmount()+",\"number\": "+provinceOrderAmount.getOrderNumber()+",\"type\": "+provinceOrderAmount.getOrderCount()+"}");
            if (i < provinceOrderAmountList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("    ],\"valueName\": \"交易额\",\"NumberName\": \"下单人数\",\"typeName\": \"下单次数\"}}");
        return jsonB.toString();
    }
}

//{\"name\": \""+provinceOrderAmount.getProvinceName()+"\",\"value\": "+provinceOrderAmount.getOrderAmount()+"}